//
//  HZHomeBottonView.swift
//  HansWeibo
//
//  Created by Hans on 16/8/13.
//  Copyright © 2016年 Hans. All rights reserved.
//

import UIKit

class HZHomeBottonView: UIView {
    
    var retweetBtn: UIButton?
    var commentBtn: UIButton?
    var unlikeBtn: UIButton?
    
    
    
    // 供外界赋值
    var homeViewModel: HZHomeViewModel?{
        didSet{
            
            
            
            // 设置title
            retweetBtn?.setTitle(homeViewModel?.repostsCountStr, forState: UIControlState.Normal)
            commentBtn?.setTitle(homeViewModel?.commentsCountStr, forState: UIControlState.Normal)
            retweetBtn?.setTitle(homeViewModel?.attitudesCountStr, forState: UIControlState.Normal)
        }
    }
    
    

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    // 设置视图
    private func setupUI(){
        
        backgroundColor = UIColor.whiteColor()
        // 添加控件
        retweetBtn = addChildButtons("timeline_icon_retweet", title: "转发")
        commentBtn = addChildButtons("timeline_icon_comment", title: "评论")
        unlikeBtn = addChildButtons("timeline_icon_unlike", title: "赞")
        let line1 = addChildLines()
        let lins2 = addChildLines()
        
        
        // 添加约束
        retweetBtn!.snp_makeConstraints { (make) in
            make.top.leading.bottom.equalTo(self)
            make.width.equalTo(commentBtn!)
        }
        commentBtn!.snp_makeConstraints { (make) in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(retweetBtn!.snp_trailing)
            make.width.equalTo(unlikeBtn!)
        }
        unlikeBtn!.snp_makeConstraints { (make) in
            make.top.bottom.trailing.equalTo(self)
            make.leading.equalTo(commentBtn!.snp_trailing)
        }
        line1.snp_makeConstraints { (make) in
            make.centerY.equalTo(self)
            make.centerX.equalTo(retweetBtn!.snp_trailing)
        }
        lins2.snp_makeConstraints { (make) in
            make.centerY.equalTo(self)
            make.centerX.equalTo(commentBtn!.snp_trailing)
        }
        
    }
    
    
    func addChildButtons(imageName: String, title: String) -> UIButton {
        let btn = UIButton()
        // 背景图片
        btn.setBackgroundImage(UIImage(named: "timeline_card_bottom_background_highlighted"), forState: UIControlState.Highlighted)
        btn.setBackgroundImage(UIImage(named: "timeline_card_bottom_background"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: imageName), forState: UIControlState.Normal)
        btn.setTitle(title, forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.grayColor(), forState: UIControlState.Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(15)
        addSubview(btn)
        return btn
    }
    
    func addChildLines() -> UIImageView{
        let lineImage = UIImageView(imageName: "timeline_card_bottom_line")
        addSubview(lineImage)
        return lineImage
    }
    
}
